package com.carlinfo.shop.common.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class MyCallable implements Callable<Object>
{

	/**
	 * call====run方法
	 * 此方法有返回值钱 返回值的类型就是Callable的泛型
	 */
	@Override
	public Object call() throws Exception
	{
		Date now = new Date();
		System.out.println(Thread.currentThread().getName() + "===>" + now.toLocaleString());
		return now.toLocaleString();
	}

	public static void main(String[] args)
	{
		System.out.println("==main开始==");
		try
		{
			/* 创建一个线程池(强烈推荐)
			 * ExecutorService:在整个项目中不要关闭;
			 *  */
			ExecutorService executorService = Executors.newCachedThreadPool();
			/* 启动一个线程
			 * 启动以后,主线程(main)不等待
			 *  */
			Future future = executorService.submit(new MyCallable());
			/* 多线程的返回值 */
			//System.out.println("====>" + future.get());
			/* 线程池要关闭 */
			executorService.shutdown();
		}catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("==main结束==");
		
		System.out.println("==main开始==");
		try
		{
			/* 创建一个线程池 */
			ExecutorService executorService = Executors.newCachedThreadPool() ; 
			/* 多线程集合,任务集合 */
			List<MyCallable> myCallableList = new ArrayList<MyCallable>() ;
			
			/* 放任务 */
			myCallableList.add(new MyCallable());
			myCallableList.add(new MyCallable());
			myCallableList.add(new MyCallable());
			
			executorService.invokeAll(myCallableList);
			/* 线程池要关闭 */
			executorService.shutdown();
		} catch (InterruptedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("==main结束==");
	}
}
